﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace UI_Layer.Pages
{
    /// <summary>
    /// Interaction logic for AccountPay.xaml
    /// </summary>
    public partial class AccountPay : Page
    {
        public AccountPay()
        {
            InitializeComponent();

            DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                this.AccountPay_Clock.Content = DateTime.Now.ToString("HH:mm:ss");
            }, this.Dispatcher);

        }

        private static int bookingNumber ;

        public static int getBookingNumber
        {
            get { return bookingNumber; }
        }

        private void ButtonClick_GoBack(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void ButtonClick_LogIn(object sender, RoutedEventArgs e)
        {
            if ((!String.IsNullOrEmpty(UserName_TextBOX.Text) && (!String.IsNullOrEmpty(Password_TextBox.Password))))
            {
                WebService.SOAPServiceClient webservice = new WebService.SOAPServiceClient();
                WebService.Users user = new WebService.Users();

                user = webservice.readUser(UserName_TextBOX.Text);
                try
                {
                    if (user.UserName.Equals(UserName_TextBOX.Text) && user.PassWord.Equals(Password_TextBox.Password))
                    {
                        int toPay = BookingDetailsCreate.getPayAmount;
                        int updatebalance = user.Balance - toPay;
                        
                        if (user.Balance >= toPay)
                        {
                            bookingNumber = Confirmation.createBooking();
                            webservice.updateUserbalance(user.UserName, updatebalance);
                            webservice.createUsersBooking(user.UserName, bookingNumber); 

                            Confirmation confirmationpage = new Confirmation();                           
                            NavigationService navservice = NavigationService.GetNavigationService(this);
                            navservice.Navigate(confirmationpage);
                        }
                        else
                        {
                            MessageBox.Show("Sorry,not enough money");
                        }

                    }
                    else
                    {
                        MessageBox.Show("Wrong Credentials. Try again.");
                    }
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("User does not exist");
                }
            }
            else
            {
                MessageBox.Show("Please fill out both fields");
            }

        }

    }
}
